package com.seocoo.worker.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.seocoo.worker.R;
import com.seocoo.worker.base.ImageLoaderEngine;
import com.seocoo.worker.base.multiImage.BaseListImageFragment;
import com.seocoo.worker.base.multiImage.RspImage;
import com.seocoo.worker.order.entity.RspOrderMaintainDetail;
import com.seocoo.worker.order.entity.RxCancelInstallSuccess;
import com.seocoo.worker.order.model.OrderMaintainDetailModel;
import com.seocoo.worker.order.req.ReqOrderMaintainDetail;
import com.seocoo.worker.order.widget.OrderDetailMaintanceAddressView;
import com.seocoo.worker.order.widget.OrderDetailParticularsView;
import com.seocoo.worker.order.widget.rsp.RspOrderDetailAddress;
import com.seocoo.worker.order.widget.rsp.RspOrderDetailParticulars;
import com.sfh.lib.event.BusEventManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * author : Ssp
 * date   : 2020/2/22 23:54
 * desc   : 订单详情
 */
public class OrderMaintainDetailActivity extends MVVMActivity {

    private OrderDetailMaintanceAddressView order_address;
    private OrderDetailParticularsView order_particulars_info;
    private OrderDetailParticularsView order_guarantee_period_info;
    private OrderDetailParticularsView order_maintain_info;
    private TextView tv_title;
    private ImageView iv_back;
    private TextView tv_cargo_type1;
    private TextView tv_cargo_name;
    private TextView tv_platform_recommend;
    private TextView tv_order_state;
    private TextView tv_order_type;
    private TextView tv_order_receiving;
    private TextView tv_remark;
    private ConstraintLayout cl_remark;
    private ConstraintLayout cl_store_detail;
    private ImageView iv_cargo_logo;
    private String maintainId;
    private String types;
    private RspOrderMaintainDetail mData;
    private BaseListImageFragment upLoadFileFragment;
    private LinearLayout ll_img;

    public static void start(Fragment context, String maintainId, String types) {
        Intent intent = new Intent(context.getActivity(), OrderMaintainDetailActivity.class);
        intent.putExtra("maintainId", maintainId);
        intent.putExtra("types", types);
        context.startActivityForResult(intent, 1000);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_maintain_detail_activity);
        maintainId = getIntent().getStringExtra("maintainId");
        types = getIntent().getStringExtra("types");
        initView();
        StatusBarUtil.setTranslucentForImageView(this, 0, tv_title);
        getViewModel(OrderMaintainDetailModel.class).refreshData(new ReqOrderMaintainDetail(maintainId));
    }

    private void initView() {

        order_address = findViewById(R.id.order_address);
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> finish());
        tv_title = findViewById(R.id.tv_title);
        order_particulars_info = findViewById(R.id.order_particulars_info);
        order_guarantee_period_info = findViewById(R.id.order_guarantee_period_info);
        order_maintain_info = findViewById(R.id.order_maintain_info);
        tv_cargo_name = findViewById(R.id.tv_cargo_name);
        tv_cargo_type1 = findViewById(R.id.tv_cargo_type1);
        tv_platform_recommend = findViewById(R.id.tv_platform_recommend);
        tv_order_type = findViewById(R.id.tv_order_type);
        tv_order_state = findViewById(R.id.tv_order_state);
        tv_order_receiving = findViewById(R.id.tv_order_receiving);
        tv_remark = findViewById(R.id.tv_remark);
        cl_remark = findViewById(R.id.cl_remark);
        iv_cargo_logo = findViewById(R.id.iv_cargo_logo);
        cl_store_detail = findViewById(R.id.cl_store_detail);
        ll_img = findViewById(R.id.ll_img);
        upLoadFileFragment = (BaseListImageFragment) getSupportFragmentManager().findFragmentById(R.id.upLoadFileFragment);
        tv_order_receiving.setOnClickListener(v -> {
            getViewModel(OrderMaintainDetailModel.class).takeMaintainOrder(maintainId);
        });
    }

    @LiveDataMatch
    public void onTakeMaintainOrderSuccess(String toast) {
        showToast(toast);
        BusEventManager.postEvent(new RxCancelInstallSuccess(true));
        finish();
    }

    @LiveDataMatch
    public void onQuerySuccess(RspOrderMaintainDetail data) {

        this.mData = data;
        if (!TextUtils.isEmpty(data.getCoverUrl())) {
            new ImageLoaderEngine(data.getCoverUrl()).error(R.drawable.order_detail_cargo_default).placeholder(R.drawable.order_detail_cargo_default).into(iv_cargo_logo);
        }

        String status = data.getOrderStatus();
        if (TextUtils.equals("1", status)) {
            tv_order_receiving.setVisibility(View.VISIBLE);
        } else {
            tv_order_receiving.setVisibility(View.GONE);
        }
        String description = data.getDescription();

        if (TextUtils.isEmpty(description) || TextUtils.equals("3", status)) {
            //问题描述为空
            cl_remark.setVisibility(View.GONE);
        } else {
            cl_remark.setVisibility(View.VISIBLE);
            tv_remark.setText(description);
        }

        tv_cargo_name.setText(data.getGoodsName());
        //营销方式1-用户推荐 2-折扣商品 3-平台推荐
        String sellType = data.getSellType();
        if (TextUtils.equals("1", sellType)) {
            sellType = "用户推荐";
        }
        if (TextUtils.equals("2", sellType)) {
            sellType = "折扣商品";
        }
        if (TextUtils.equals("3", sellType)) {
            sellType = "平台推荐";
        }

        tv_platform_recommend.setText(sellType);
        //维修单类型1-平台商品 2-其他商品
        String maintainType = "";
        if (TextUtils.equals("1", data.getMaintainType())) {
            maintainType = "平台商品";
            cl_store_detail.setVisibility(View.VISIBLE);
            order_guarantee_period_info.setVisibility(View.VISIBLE);
            order_particulars_info.setVisibility(View.VISIBLE);
        }
        if (TextUtils.equals("2", data.getMaintainType())) {
            maintainType = "其他商品";
            cl_store_detail.setVisibility(View.GONE);
            order_guarantee_period_info.setVisibility(View.GONE);
            order_particulars_info.setVisibility(View.GONE);
        }
        tv_cargo_type1.setText(maintainType);
        tv_order_type.setText(types);
        initOrderDetailAddressViewData(data);
        initOrderGuaranteePeriodInfoViewData(data);
        initOrderDetailMeasureViewData(data);
        initOrderDetailCommodityViewData(data);
        String[] picList = data.getPicList();
        if (picList == null || picList.length == 0) {
            ll_img.setVisibility(View.GONE);
        } else {
            ll_img.setVisibility(View.VISIBLE);
            assert upLoadFileFragment != null;
            upLoadFileFragment.setImageCount(3, picList.length, false);
            upLoadFileFragment.setTv_title1("现场图片上传");
            upLoadFileFragment.setTv_title2("");
            ArrayList<RspImage> list = new ArrayList<>();
            for (String s : picList) {
                RspImage rspImage = new RspImage();
                rspImage.setFullPath(s);
                list.add(rspImage);
            }
            upLoadFileFragment.setNewData(list);
        }
    }

    private void initOrderDetailAddressViewData(RspOrderMaintainDetail data) {
        RspOrderDetailAddress address = new RspOrderDetailAddress();
        address.setUserAddress(data.getProvince() + data.getCity() + data.getDistrict() + data.getCommunity() + data.getDoorPlate());
        address.setUserName(data.getConsignee());
        address.setUserPhone(data.getContact());
        address.setMerchantContact(data.getMerchantContact());
        address.setMaintainType("");
        order_address.initData(address);
    }

    private void initOrderGuaranteePeriodInfoViewData(RspOrderMaintainDetail data) {
        order_particulars_info.setTv_title("订单详情");
        List<RspOrderDetailParticulars> list = new ArrayList<>();
        RspOrderDetailParticulars rsp1 = new RspOrderDetailParticulars();
        rsp1.setName("订单编号：");
        rsp1.setContent(data.getOrderNo());
        list.add(rsp1);
        RspOrderDetailParticulars rsp2 = new RspOrderDetailParticulars();
        rsp2.setName("下单时间：");
        rsp2.setContent(data.getAddTime());
        list.add(rsp2);

        String payTime = data.getPayTime();
        if (!TextUtils.isEmpty(payTime)) {
            RspOrderDetailParticulars rsp3 = new RspOrderDetailParticulars();
            rsp3.setName("付款时间：");
            rsp3.setContent(payTime);
            list.add(rsp3);
        }
        String channelType = data.getChannelType();
        // 支付方式 1-支付宝 2-微信
        if (TextUtils.equals("1", channelType)) {
            RspOrderDetailParticulars rsp5 = new RspOrderDetailParticulars();
            rsp5.setName("付款方式：");
            channelType = "支付宝支付";
            rsp5.setContent(channelType);
            list.add(rsp5);
        }
        if (TextUtils.equals("2", channelType)) {
            RspOrderDetailParticulars rsp5 = new RspOrderDetailParticulars();
            rsp5.setName("付款方式：");
            channelType = "微信支付";
            rsp5.setContent(channelType);
            list.add(rsp5);
        }
        String finishTime = data.getFinishTime();
        if (!TextUtils.isEmpty(finishTime)) {
            RspOrderDetailParticulars rsp5 = new RspOrderDetailParticulars();
            rsp5.setName("成交时间：");
            rsp5.setContent(finishTime);
            list.add(rsp5);
        }
        order_particulars_info.initData(list);
    }

    private void initOrderDetailCommodityViewData(RspOrderMaintainDetail data) {

        order_guarantee_period_info.setTv_title("质保期信息");
        List<RspOrderDetailParticulars> list = new ArrayList<>();

        RspOrderDetailParticulars rsp2 = new RspOrderDetailParticulars();
        rsp2.setName("商品状态：");
        String expirationState = data.getExpirationState();//质保期状态1-质保中 2-已过期
        if (TextUtils.equals("1", expirationState)) {
            expirationState = "质保中";
            RspOrderDetailParticulars rsp1 = new RspOrderDetailParticulars();
            rsp1.setName("质保期时长：");
            String expiration = data.getExpiration();
            rsp1.setContent(expiration + "个月");
            list.add(rsp1);
        }
        if (TextUtils.equals("2", expirationState)) {
            expirationState = "已过期";
        }
        rsp2.setContent(expirationState);
        list.add(rsp2);
        order_guarantee_period_info.initData(list);
    }

    private void initOrderDetailMeasureViewData(RspOrderMaintainDetail data) {

        order_maintain_info.setTv_title("维修单信息");
        List<RspOrderDetailParticulars> list = new ArrayList<>();
        RspOrderDetailParticulars rsp1 = new RspOrderDetailParticulars();
        rsp1.setName("维修费用：");
        String costType = data.getCostType();
        if (TextUtils.equals(costType, "1")) {
            //费用类型1-无偿维修 2-有尝维修
            costType = "无偿维修（平台线下结算）";
        } else {
            costType = "有尝维修（请提前联系用户沟通维修费用）";
        }
        rsp1.setContent(costType);
        list.add(rsp1);
        RspOrderDetailParticulars rsp2 = new RspOrderDetailParticulars();
        rsp2.setName("状态：");
        String orderStatus = data.getOrderStatus();//状态0-取消 1-待确认 2-进行中 3-已完成
        if (TextUtils.equals("0", orderStatus)) {
            orderStatus = "取消";
        }
        if (TextUtils.equals("1", orderStatus)) {
            orderStatus = "待确认";
        }
        if (TextUtils.equals("2", orderStatus)) {
            orderStatus = "进行中";
        }
        if (TextUtils.equals("3", orderStatus)) {
            orderStatus = "已完成";
        }
        rsp2.setContent(orderStatus);
        tv_order_state.setText(orderStatus);
        list.add(rsp2);
        RspOrderDetailParticulars rsp3 = new RspOrderDetailParticulars();
        rsp3.setName("创建时间：");
        rsp3.setContent(data.getCreatedTime());
        list.add(rsp3);
        order_maintain_info.initData(list);
    }
}
